package com.farsoon.child.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.farsoon.child.dto.UserReqDTO;
import com.farsoon.child.entity.User;
import com.farsoon.child.dao.UserMapper;
import com.farsoon.child.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//
//import org.apache.poi.hssf.usermodel.HSSFRow;
//import org.apache.poi.hssf.usermodel.HSSFSheet;
//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Objects;

import static com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility.getBytes;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ag1usee
 * @since 2024/04/07
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService, Runnable {

    @Resource
    UserMapper userMapper;

    private static final String XLSX = ".xlsx";
    private static final String XLS = ".xls";

    @Override
    public void saveOrUser() {
        UserServiceImpl mUserServiceImpl = new UserServiceImpl();
        Thread mThread = new Thread(mUserServiceImpl);
        mThread.start();

//        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
//        List<User> list = list(queryWrapper);
//        userMapper.selectList(queryWrapper).forEach(u -> {
//            System.out.println(u.getAddress());
//            System.out.println(u.getName());
//            System.out.println(u.getAge());
//        });

//        if (list.size() > 0) {
//            User user = list.get(0);
//            System.out.println(user);
//        }else {
//            User user = new User();
//            user.setAge("123");
//            user.setAddress("road");
//            user.setName("wa");
//            BeanUtils.copyProperties(user, user);
//            save(user);
//        }
    }

    @Transactional
    @Override
    public void uploadFile(UserReqDTO reqDTO, MultipartFile file) {
        try {
            // 解析表格数据
            InputStream in;
            String fileName;
            // 上传文件解析
            in = file.getInputStream();
            fileName = file.getOriginalFilename();
            //  获取第一个sheet
            Workbook book = null;

            Sheet sheet = book.getSheetAt(0);

            // 读取第一张sheet
//            HSSFSheet sheet = workbook.getSheetAt(0);
            Row row1 = sheet.getRow(1);
            Row row2 = sheet.getRow(2);
            // 遍历每一行
            {
                // 读取每一列数据
                String OriginScaleX = row1.getCell(2).getStringCellValue();
                String OriginScaleY = row2.getCell(2).getStringCellValue();
                String ScaleX = row1.getCell(6).getStringCellValue();
                String ScaleY = row2.getCell(6).getStringCellValue();

                // 将数据存入数据库
                User mUser = new User();
                BeanUtils.copyProperties(reqDTO, mUser);
                mUser.setOriginScaleX(OriginScaleX);
                mUser.setOriginScaleY(OriginScaleY);
                mUser.setScaleX(ScaleX);
                mUser.setScaleY(ScaleY);
                saveOrUpdate(mUser);
                // 调用数据库操作方法将数据存入数据库
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

//        User mUser = null;
//        if (Objects.nonNull(reqDTO)) {
////            MultipartFile avatar = reqDTO.getAvatar();
//            if (Objects.nonNull(file)) {
//                try {
//                    InputStream inputStream = file.getInputStream();
//                    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
//                    // 读取第一张sheet
//                    HSSFSheet sheet = workbook.getSheetAt(0);
//                    HSSFRow cOneRow = sheet.getRow(1);
//                    if (cOneRow != null) {
//                        mUser = new User();
//                        BeanUtils.copyProperties(reqDTO, mUser);
//                        cOneRow.getCell(3).setCellType(CellType.STRING);
//                        cOneRow.getCell(6).setCellType(CellType.STRING);
//                        mUser.setAge(cOneRow.getCell(3).getStringCellValue());
//                        mUser.setAddress(cOneRow.getCell(6).getStringCellValue());
//                    }
////                    saveOrUpdate(mUser);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
    }
    
    @Override
    public void run() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时器任务执行中...");
            }
        };

        // 每隔5秒执行一次定时器任务
        timer.schedule(task, 0, 5000);

    }
}
